package org.game.netty.server;

import org.bean.InterceptInfoDTO;
import org.cache.InterceptMappingCache;
import org.constant.InterceptConstant;
import org.error.GameErrorCode;
import org.game.netty.oldcoder.other.IChannelConstants;

import com.sojoys.artifact.factory.message.IMessage;
import com.sojoys.artifact.factory.session.ISession;
import com.sojoys.artifact.manager.MessageManager;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 消息拦截处理
 * Created by fxf on 2017/12/18.
 */
public class InterceptHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        
        
        
        boolean intercept = false;
        if (msg instanceof IMessage) {
        	IMessage imsg = (IMessage) msg;
        	ISession session = ctx.channel().attr(IChannelConstants.SESSION).get();
            // 检查拦截消息号
            InterceptInfoDTO dto = InterceptMappingCache.checkIntercept(InterceptConstant.INTERCEPT_TYPE_MESSAGE_CODE, String.valueOf(imsg.getCommandId()));
            if (dto!=null) {
            	intercept = true;
            	IMessage out = MessageManager.me().create(imsg.getCommandId(), GameErrorCode.INTERCEPT_MESSAGE_CODE);
                session.sendMessage(out);
			}
        }
        
        if (!intercept) {
            ctx.fireChannelRead(msg);
        }

    }
}
